import { listSimpleDictDatas } from '@/api/system/dict/data'
import user from '@/store/modules/user'

const dict = {
    state: {
        dictDatas: {}
    },
    mutations: {
        SET_DICT_DATAS: (state, dictDatas) => {
            state.dictDatas = dictDatas
        }
    },
    actions:{
        loadDictDatas({ commit }) {
            listSimpleDictDatas().then(response => {
                // console.log('response', response)
                // 如果未加载到数据，则直接返回
                if (!response || !response.data) {
                    return
                }
                // 设置数据
                const dictDataMap = {}
                response.data.forEach(dictData => {
                    // 获得 dictType 层级
                    const enumValueObj = dictDataMap[dictData.dictType]
                    if (!enumValueObj) {
                        dictDataMap[dictData.dictType] = []
                    }
                    // 处理 dictValue 层级
                    dictDataMap[dictData.dictType].push({
                        value: dictData.value,
                        label: dictData.label,
                        colorType: dictData.colorType,
                        cssClass: dictData.cssClass
                    })
                })
                // 存储到 Store 中
                commit('SET_DICT_DATAS', dictDataMap)
            })
        }
    }
}

export default dict
